package com.feib.soeasy.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.MenuItemDao;
import com.feib.soeasy.model.LabelValue;
import com.feib.soeasy.model.MenuItem;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.MenuItemService;

/**
 * @title (#)MenuItemServiceImpl.java<br>
 * @description <br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/4 create by Anson Tsai<br>
 */
@Service("menuItemService")
public class MenuItemServiceImpl extends BaseServiceImpl implements MenuItemService {

    @Resource(name = "menuItemDao")
    private MenuItemDao menuItemDao;

    /*
     * (non-Javdoc)
     * 
     * @see com.feib.soeasy.service.MenuItemService#getAllMenuItem()
     */
    @Override
    public List<MenuItem> getAllMenuItemOrderMenuId() {
        return menuItemDao.findAllOrderMenuId();
    }
    
    /* (non-Javadoc)
     * @see com.feib.soeasy.service.MenuItemService#getSchoolMenuItemOrderMenuId()
     */
    @Override
    public List<MenuItem> getSchoolMenuItemOrderMenuId() {
        return menuItemDao.findSchoolOrderMenuId();
    }
    
    /* (non-Javadoc)
     * @see com.feib.soeasy.service.MenuItemService#getCommitteeMenuItemOrderMenuId()
     */
    @Override
    public List<MenuItem> getCommitteeMenuItemOrderMenuId() {
        return menuItemDao.findCommitteeOrderMenuId();
    }

    @Override
    public Set<MenuItem> findMenuItem(Set<MenuItem> menuItems) {
        if (CollectionUtils.isNotEmpty(menuItems)) {
            Set<MenuItem> menus = new HashSet<MenuItem>();
            for (MenuItem m : menuItems) {
                menus.add(menuItemDao.findByPk(m.getMenuItemPk()));
            }
            return menus;
        }
        return null;
    }

    @Override
    public PaginatedList<MenuItem> queryByConditions(String menuType, String menuId, String actionName, PaginatedList<MenuItem> paginationConfig) {
        
        return menuItemDao.queryPaginationByConditions(menuType, menuId, actionName, paginationConfig);
    }

    @Override
    public MenuItem getMenuItemByPk(Long menuItemPk) {
        
        return menuItemDao.findByPk(menuItemPk);
    }

    @Override
    public List<LabelValue> findLabelValueByParentMenuItem() {
        List<LabelValue> result = new ArrayList<LabelValue>();
        List<MenuItem> itmes = menuItemDao.findParentMenuItem();        
        for (MenuItem m : itmes) {
            LabelValue lv = new LabelValue();
            lv.setLabel(m.getActionName());
            lv.setValue(m.getMenuItemPk().toString());
            result.add(lv);
        }

        return result;        
    }

    @Override
    public MenuItem save(MenuItem m) {
        
        return menuItemDao.save(m);
    }

    @Override
    public void delete(MenuItem m) {
        menuItemDao.delete(m);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.MenuItemService#queryAll()
     */
    @Override
    public List<MenuItem> queryAllLazyRoles() {
        return this.menuItemDao.findAllMenuItemLazyRoles();
    }

}
